package com.ludomancy.spaceopera;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;

import com.ludomancy.gamelib.BarPainter;
import com.ludomancy.gamelib.ResourceManager;
import com.ludomancy.gamelib.Utils;
import com.ludomancy.gamelib.Widget;


public class StatusWidget extends Widget
{
	private static final Rectangle SHIELD_BAR_POS = new Rectangle(20,12,32,5);
	private static final Rectangle CARGO_BAR_POS = new Rectangle(20,24,32,5);
	private static final Rectangle ITEM_BAR_POS = new Rectangle(15,50,30,30);
	
	public StatusWidget(Widget parent, Rectangle area)
	{
		super(parent, area);
	}

	public void paint(Graphics g)
	{
		g.drawImage(ResourceManager.getImage("hud.png"), 0, 0, null);		
		BarPainter.paint(g, new Color(0xC9C614), SHIELD_BAR_POS.x, SHIELD_BAR_POS.y, SHIELD_BAR_POS.width, SHIELD_BAR_POS.height, Game.game.player.getShield(), Game.game.player.getMaxShield());
		// picture costs
		if(Game.game.player.interactableEntity instanceof StationButton)
		{
			StationButton button = (StationButton)Game.game.player.interactableEntity;
			int cost = button.getCost();
			if(button.canAfford())
			{
				Rectangle cargo = BarPainter.paint(g, new Color(0x645CF2), CARGO_BAR_POS.x, CARGO_BAR_POS.y, CARGO_BAR_POS.width, CARGO_BAR_POS.height, Game.game.player.getCargo()-cost, Game.game.player.maxCargo);				
				BarPainter.paint(g, new Color(0x6C0400), CARGO_BAR_POS.x+cargo.width, CARGO_BAR_POS.y, CARGO_BAR_POS.width, CARGO_BAR_POS.height, cost, Game.game.player.maxCargo);
			}
			else
			{
				BarPainter.paint(g, new Color(0x6C0400), CARGO_BAR_POS.x, CARGO_BAR_POS.y, CARGO_BAR_POS.width, CARGO_BAR_POS.height, cost, Game.game.player.maxCargo);
			}
		}
		else
		{
			BarPainter.paint(g, new Color(0x645CF2), CARGO_BAR_POS.x, CARGO_BAR_POS.y, CARGO_BAR_POS.width, CARGO_BAR_POS.height, Game.game.player.getCargo(), Game.game.player.maxCargo);
		}
		if(Game.game.player.inventory != null)
		{
			Point itemSize = Game.game.defaultProjector.worldToScreen(new Point(Game.game.player.inventory.getWidth(), Game.game.player.inventory.getHeight()));
			Point p = Utils.getCenterInRect(itemSize, ITEM_BAR_POS);
			Game.game.player.inventory.paint(g, p.x, p.y);
			if(Game.game.player.canActivateItem())
			{
				SpaceWidget.paintOutline(g, "", ITEM_BAR_POS.x, ITEM_BAR_POS.y, ITEM_BAR_POS.width, ITEM_BAR_POS.height);
			}
			
		}
	}
}
